module.exports = {
	/**
	 * 查询角色列表
	 * @url client/wechat/selects/pub/selects 前端调用的url参数地址
	 * 表名
	 * @dbName 查询的表的名称
	 * data 请求参数 说明
	 * @param {String}         _id  用户编号
	 * res 返回参数说明
	 * @param {Number}         code      错误码，0表示成功
	 * @param {String}         msg       详细信息
	 */
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid } = data;
		let res = { code: 0, msg: '' };
		// 业务逻辑开始-----------------------------------------------------------
		let {_id} = data;
		let { todayStart, todayEnd } = vk.pubfn.getCommonTime();
		var num = await vk.baseDao.count({
			dbName:"uni-id-scores",
			pageIndex:1,
			pageSize:1,
			whereJson:{
				user_id: _id,
				_add_time : _.gte(todayStart).lte(todayEnd),
				comment: '每日签到'
			}
		});
		if(num > 0){
			res.code = -1
			res.msg = "每天只能签到一次~"
			return res;
		}
		//查询签到应该获得积分
		var signScore = await vk.baseDao.findByWhereJson({
			dbName: 'bus-wx-config',
			whereJson: {
				config_key: 'score_config'
			}
		});
		if(signScore.sign_score <= 0){
			res.code = -1
			res.msg = "未开启签到"
			return res;
		}
		// 执行数据库API请求
		var user = await vk.baseDao.updateById({
			dbName: "uni-id-users",
			id: _id,
			dataJson: {
				score: _.inc(signScore.sign_score)
			},
			getUpdateData:true
		});
		if(!user){
			res.code = -1
			res.msg = "签到失败"
			return res;
		}
		
		var scoreNum = await vk.baseDao.add({
			dbName: "uni-id-scores",
			dataJson: {
				user_id: _id,
				type: 1,
				score: signScore.sign_score,
				balance: user.score,
				comment: "每日签到"
			}
		});
		
		res.score = signScore.sign_score
		res.user = user
		
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}

}